package com.qf.ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.ssm.cache.AreasCache;
import com.qf.ssm.dto.ResultCode;
import com.qf.ssm.event.UpdateAreaCacheEvent;
import com.qf.ssm.exceptions.AddErrorException;
import com.qf.ssm.exceptions.DeleteErrorException;
import com.qf.ssm.exceptions.MybaseException;
import com.qf.ssm.exceptions.UpdateErrorException;
import com.qf.ssm.mapper.AreasMapper;
import com.qf.ssm.mapper.CategoriesMapper;
import com.qf.ssm.pojo.Areas;
import com.qf.ssm.pojo.Categories;
import com.qf.ssm.pojo.CheckNull;
import com.qf.ssm.pojo.CheckStatus;
import com.qf.ssm.service.AreasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.naming.Context;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class AreasServiceImpl implements AreasService {

    private AreasMapper areasMapper;

    @Autowired
    public void setAreasMapper(AreasMapper areasMapper) {
        this.areasMapper = areasMapper;
    }
    private AreasCache areasCache;

    private ApplicationContext context;

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Autowired
    public void setAreasCache(AreasCache areasCache){
        this.areasCache=areasCache;
    }

    @Override
    public void addAreas(Areas areas) {
        if(areas.isNull(CheckStatus.ADD)){
            throw new AddErrorException("添加的数据不能为空", ResultCode.DATA_NOT_ALLOW_NUL);
        }
        areasMapper.addAreas(areas);
        //需要更新缓存
        //areasCache.init();
        context.publishEvent(new UpdateAreaCacheEvent());
    }

    @Override
    public List<Areas> findParentAreaByStatus(Long status) {
        return areasMapper.findParentAreaByStatus(status);
    }

    @Override
    public PageInfo<Areas> findAllAreasByNameLikeAndStatusEquals(int page, int limit, String areaname, Long status) {
        PageHelper.startPage(page,limit);
       // List<Areas> areasList = areasMapper.findAllAreasByNameLikeAndStatusEquals(areaname,status);
        List<Areas> areasList = areasCache.getAllData(); //从缓存中获取数据
       // return new PageInfo<>(areasList);

        List<Areas> subList = areasList.stream().filter(areas -> {
            // boolean b1 = ObjectUtils.isEmpty(areaname) && ObjectUtils.isEmpty(status);
            boolean b2= ObjectUtils.isEmpty(areaname)?true:areas.getAreaname().contains(areaname);
            boolean b3 = (ObjectUtils.isEmpty(status)||status==-100)? true : areas.getStatus().equals(status);
            return  b2 && b3;

        }).skip((page - 1) * limit).limit(limit).collect(Collectors.toList());
        PageInfo<Areas> pageInfo = new PageInfo<>(subList);
        pageInfo.setTotal(areasList.size());

        return pageInfo;
    }

    @Override
    public int deleteAreasByIds(Long[] ids) {
        if(ids==null || ids.length==0){
            //删除时必须又id
            throw  new DeleteErrorException("删除必须传递 id", ResultCode.DATA_NOT_ALLOW_NUL);
        }
        return areasMapper.deleteAreasByIds(Arrays.asList(ids));
    }

    @Override
    public int updateAreas(Areas areas) {
        if(areas.isNull(CheckStatus.UPDATE)){
            throw new UpdateErrorException("更新的数据为空", ResultCode.DATA_NOT_ALLOW_NUL);
        }
        return areasMapper.updateAreas(areas);
    }

    @Override
    public Areas getAreasById(Long id) {

        return areasCache.get(id);
    }

    @Override
    public List<Areas> getAreaByParentId(Long id) {
        if(id==null || id==0){
            throw new MybaseException("没有传递id",ResultCode.DATA_NOT_ALLOW_NUL);
        }
        try {
            // List<Area> areaList = areaMapper.getAreaByParentId(id);
            List<Areas> allData = areasCache.getAllData();//从缓存查询所有数据
            List<Areas> areaList = allData.stream().filter(area -> id == area.getParentid()).collect(Collectors.toList());//获取父id和当前id一样的数据
            return areaList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MybaseException("查询失败", ResultCode.ERROR);

        }
    }

    @Override
    public List<Areas> findParentAreaByStatusLong(Long status) {
        return findParentAreaByStatus(status);
    }


}
